//
//  MediaViewController.m
//  OfficeEtc
//
//  Created by Gui Wei on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "MediaViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "WifiViewController.h"

@interface MediaViewController ()

- (void)addPhotoAction;

@end

@implementation MediaViewController

@synthesize imagePickerController = _imagePickerController;
@synthesize albumPickerController = _albumPickerController;
@synthesize elcPickerController = _elcPickerController;


#pragma mark -
#pragma mark UIViewControl Methods

-(void)initImagePickerController
{
	self.imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
	_imagePickerController.delegate = self;
}

//初始化asset照片库浏览视图
-(void)initELCAssetViewController
{
    self.albumPickerController = [[[ELCAlbumPickerController alloc] initWithNibName:@"ELCAlbumPickerController" bundle:[NSBundle mainBundle] type: _sourceType] autorelease];  
    
	self.elcPickerController = [[[ELCImagePickerController alloc] initWithRootViewController:_albumPickerController] autorelease];
    [_albumPickerController setParent:_elcPickerController];
	[_elcPickerController setDelegate:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    [self initImagePickerController];
    [self initELCAssetViewController];
    
}

- (void)dealloc
{
    TT_RELEASE_SAFELY(_imagePickerController);
    TT_RELEASE_SAFELY(_albumPickerController);
    TT_RELEASE_SAFELY(_elcPickerController);
    [super dealloc];
}

#pragma mark -
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setupImagePicker:(UIImagePickerControllerSourceType)sourceType
{
    _imagePickerController.sourceType = sourceType;
    if(_sourceType == kSourceVideo)
    {
        [_imagePickerController setMediaTypes:[NSArray arrayWithObject:@"public.movie"]];
        _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    }
	
    _imagePickerController.showsCameraControls = YES;
}

- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
    if ([UIImagePickerController isSourceTypeAvailable:sourceType])
    {
		[self setupImagePicker:sourceType];
        [self presentModalViewController:_imagePickerController animated:YES];
    }
}

//打开照片库
-(void)photoLibraryAction
{
    [self presentModalViewController:_elcPickerController animated:YES];
}

//打开摄像头
- (void)cameraAction
{
    [self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}

#pragma mark - 
#pragma mark Photo Methods
- (void)addPhotoAction
{
    UIActionSheet *photoSheet = [[UIActionSheet alloc] init];
    photoSheet.delegate = self;
    
    NSMutableArray* actionArray = [NSMutableArray arrayWithObjects:NSLocalizedString(@"Photo Libary", @""), NSLocalizedString(@"Photo Wifi", @""),NSLocalizedString(@"Photo ftp", @""), NSLocalizedString(@"Cancel", @""), nil];
    
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) //有照相机设备
	{
        [actionArray insertObject:NSLocalizedString(@"Photo Camera", @"") atIndex:0];
	}
    
    for(NSString* buttonTitle in actionArray)
    {
        [photoSheet addButtonWithTitle:buttonTitle];
    }
    
    photoSheet.cancelButtonIndex = photoSheet.numberOfButtons-1;
    [photoSheet showInView:self.view];
    [photoSheet release];
}

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info {
    
    for(NSDictionary *dict in info)
    {
        /*
         key:@"SourceAsset", 值类型：ALAsset*(该资源的asset)
         key:@"image_ext", 字符串：资源后缀名，比如jpg,png等
         
         获得图像：
         if([[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0)
         {
         [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]]
         }
         else
         {
         [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage] scale:1.0 orientation:[[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue]]
         }
         
         获得缩略图(75*75或150*150)，显示的cell用75差不多，如果缩略图生成成功，可以参考ELCAsset生成缩略图的tableview，节约时间
         ALAsset:thumbnail
         
         ex:UIImage* thumbImage = [UIImage imageWithCGImage:[self.asset thumbnail]];
         [workingDictionary setObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]] forKey:@"UIImagePickerControllerOriginalImage"];
         */
        
    }
    
	[self dismissModalViewControllerAnimated:YES];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker {
    
	[self dismissModalViewControllerAnimated:YES];
}

- (void)elcImagePickerControllerDidAdd:(ELCImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UIImagePickerControllerDelegate

// this get called when an image has been chosen from the library or taken from the camera
//
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    
    
	[_imagePickerController dismissModalViewControllerAnimated:YES];
	
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}


#pragma mark -
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString* buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
    if([buttonTitle isEqualToString:NSLocalizedString(@"Photo Libary", @"")])
    {
        [self photoLibraryAction];
    }
    else if([buttonTitle isEqualToString:NSLocalizedString(@"Photo Wifi", @"")])
    {
        UIBarButtonItem* tBar = [[[UIBarButtonItem alloc] init] autorelease];
        tBar.tag =30;
        [self performSelector:@selector(toolButtonPressed:) withObject:tBar];
    }
    else if([buttonTitle isEqualToString:NSLocalizedString(@"Photo Camera", @"")])
    {
        [self cameraAction];
    }else if([buttonTitle isEqualToString:NSLocalizedString(@"Photo ftp", @"")])
    {
        UIBarButtonItem* tBar = [[[UIBarButtonItem alloc] init] autorelease];
        tBar.tag =31;
        [self performSelector:@selector(toolButtonPressed:) withObject:tBar];
    }
    
    
}


@end
